#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#include <immintrin.h>
//#include <emmintrin.h>

#define ls(x) ((x)*2)
#define rs(x) ((x)*2+1)
#define fi first
#define se second
#define pii pair<int,int>
#define Debug(...) fprintf(stderr, __VA_ARGS__)
#define For(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define Rof(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define rep(i,  b) for(int i=1,i##end=b;i<=i##end;i++)
using namespace std;
const int N=1e6+5,base=999983,Mod=1e9+7;
//char buf[(1<<21)+5],*p1,*p2;
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline void chmx(int &x,int y){(x<y)&&(x=y);}
inline void chmn(int &x,int y){(x>y)&&(x=y);}
inline void Add(int &x,int y){(x=x+y+Mod)%=Mod;}
inline int read(){
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return f?-x:x;
}
void print(int n){
    if(n<0){
        putchar('-');
        n*=-1;
    }
    if(n>9) print(n/10);
    putchar(n%10+'0');
}
int n,k;
int w[N];
int C[5005][5005];
int S[N],P[N];
vector<int>q[N];
int use[N];
int siz[N];
int ans=0; 
int RT=0;
int f[N]; 
void dfs(int x,int faz){
	siz[x]=1;
	for(auto y:q[x]){
		if(y==faz) continue;
		dfs(y,x);
		siz[x]+=siz[y];
	}
	if((use[x]&RT)!=use[x]){
		siz[x]=0;return ;
	}
	(f[RT]+=P[siz[x]])%=Mod;
	for(auto y:q[x]){
		if(y==faz)continue;
		(f[RT]+=Mod-P[siz[y]])%=Mod;
	}
}
int p[N]; 
signed main(){
	freopen("water.in","r",stdin);
	freopen("water.out","w",stdout);
	// ios::sync_with_stdio(false);
	// cin.tie(0); cout.tie(0);
	n=read(),k=read();
	For(i,1,k) w[i]=read();
	S[0]=1;
	C[1][1]=1;
	For(i,1,n){
		For(j,1,i){
			(C[i+1][j+1]+=C[i][j])%=Mod;
			(C[i+1][j]+=1ll*C[i][j]*j%Mod)%=Mod;
			(S[i]+=C[i][j])%=Mod;
			C[i][j]=0;
		}
	}
	C[0][0]=1;
	For(i,1,n){
		C[i][0]=1;
		For(j,1,i){
			C[i][j]=(C[i-1][j]+C[i-1][j-1])%Mod;
			(P[i]+=1ll*C[i][j]*S[n-j]%Mod)%=Mod;
		}
	}
	For(i,1,n){
		int len=read();
		For(j,1,len){
			int x=read();
			use[i]|=(1<<(x-1));
		}
	}
	For(i,1,n-1){
		int u=read(),v=read();
		q[u].push_back(v);
		q[v].push_back(u);
	}
	For(i,0,(1<<k)-1){
		RT=i;
		dfs(1,0);
		
	}
	For(i,0,(1<<k)-1){
		For(j,0,i-1){
			if((i&j)==j){
				f[i]+=Mod-f[j];f[i]%=Mod;
			}
		}
//		cout<<i<<" "<<f[i]<<endl;
		ans+=1ll*f[i]*w[__builtin_popcount(i)]%Mod;ans%=Mod;
	} 
	printf("%d\n",ans);
#ifdef LOCAL
    Debug("\nMy Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}

